//
//  MTViewController.m
//  ScrollView练习一个需求
//
//  Created by 蒋俊 on 16/8/28.
//  Copyright © 2016年 蒋俊. All rights reserved.
//

#import "MTViewController.h"

@interface MTViewController () <UIScrollViewDelegate>

/**<#注释#>*/
@property (nonatomic , strong) UIView *topBar;


@end

@implementation MTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //快速创建个scrollView
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = self.view.bounds;
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, 1500);
    scrollView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:scrollView];
    scrollView.delegate = self;
    
    //在ScrollView（0，0）位置加个开关让你看清楚
    UISwitch *MJswitch = [[UISwitch alloc]init];
    MJswitch.frame = CGRectMake(0, 0, 100, 50);
    [scrollView addSubview:MJswitch];
    
    //顶部条
    UIView *topBar = [[UIView alloc]init];
    topBar.frame = CGRectMake(0 ,70 ,self.view.bounds.size.width ,50 );
    topBar.backgroundColor = [UIColor redColor];
    [scrollView addSubview:topBar];
    self.topBar = topBar;


}

#pragma mark -- <UIScrollViewDelegate>方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //思路一:当我们监听到顶部条随着scrollView滑到顶部后，就让顶部条直接以当前位置加到self.view身上去，当下滑时，又加回到原来scrollView上去
  //注意:scrollView往上滑动，偏移量Y值是个正数
    //if (scrollView.contentOffset.y >= 70) {
//        //重新设置顶部条的frame，并且加到父控件上去
//        self.topBar.frame = CGRectMake(0, 0, self.view.bounds.size.width ,50);
//        [self.view addSubview:self.topBar];
//    }else
//    {
//        self.topBar.frame = CGRectMake(0 ,70 ,self.view.bounds.size.width ,50 );
//        [scrollView addSubview:self.topBar];
//    }
   //思路二:我们可以在顶部控件到达顶部时，跟随着scrollView改变其y值，让其看似在那里是不动的
    if (scrollView.contentOffset.y >= 70) {
        CGRect topF = self.topBar.frame;
        topF.origin.y = scrollView.contentOffset.y;
        self.topBar.frame = topF;
        
    }

    
    
}







@end
